package com.wx.app.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.newemptyapplication.R;
import com.wx.app.dao.DaoHelper;
import com.wx.app.dao.Note;
import com.wx.app.dao.NoteDao;
import com.wx.app.mylib.app.BaseActivity;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemClick;
import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;

public class GreenDaoActivity extends BaseActivity {

    private NoteDao noteDao;
    ArrayList<Note> noteList = new ArrayList<>();
    private MyAdapter myAdapter;
    @BindView(R.id.et_note)
    EditText editText;
    @BindView(R.id.tv_show)
    TextView tvShow;
    @BindView(R.id.btn_add)
    Button btnAdd;
    @BindView(R.id.btn_query)
    Button btnQuery;
    @BindView(R.id.lv_show)
    ListView lvShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_green_dao);
        ButterKnife.bind(mActivity);
        noteDao = DaoHelper.getInstance().getNoteDao();
//        initView();
        myAdapter = new MyAdapter();
        lvShow.setAdapter(myAdapter);
    }

    @OnItemClick(R.id.lv_show) void onItemClick(int position){
        Note note = noteList.get(position);
//                noteDao.deleteByKey(id);
        noteDao.delete(note);
        noteList.remove(position);
        myAdapter.notifyDataSetChanged();
        showToast("删除一条数据, ID: " + Long.toString(position));
    }



    @OnClick(R.id.btn_add)
    void addNote() {
        String noteText = editText.getText().toString();
        editText.setText("");
        noteList.clear();
        final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
        String comment = "Added on " + df.format(new Date());

        // 插入操作，简单到只要你创建一个 Java 对象
        Note note = new Note(null, noteText, comment, new Date());
        noteDao.insert(note);
        QueryBuilder<Note> queryBuilder = noteDao.queryBuilder();
        List<Note> list = queryBuilder.list();
        for (Note note1 : list) {
            noteList.add(note1);
        }

        myAdapter.notifyDataSetChanged();
        showToast("Inserted new note, ID: " + note.getId());
    }

    @OnClick(R.id.btn_query)
    void search() {
        tvShow.setText("");
        // Query 类代表了一个可以被重复执行的查询
        Query query = DaoHelper.getInstance().getNoteDao().queryBuilder()
                .where(NoteDao.Properties.Text.eq(editText.getText().toString()))
                .orderAsc(NoteDao.Properties.Date)
                .build();

        //      查询结果以 List 返回
        List<Note> notes = query.list();
        // 在 QueryBuilder 类中内置两个 Flag 用于方便输出执行的 SQL 语句与传递参数的值
        QueryBuilder.LOG_SQL = true;
        QueryBuilder.LOG_VALUES = true;
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < notes.size(); i++) {
            sb.append(notes.get(i).getText() + " " + notes.get(i).getComment() + "---");
        }
        tvShow.setText(sb);
        showToast( "查询结果数: " + notes.size());
    }

    public class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return noteList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MyViewHolder myViewHolder;
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.item_green_dao_show, null);
                myViewHolder = new MyViewHolder(convertView);
                convertView.setTag(myViewHolder);
            } else {
                myViewHolder = (MyViewHolder) convertView.getTag();
            }
            if (noteList.size() > 0) {
                Note note = noteList.get(position);
                myViewHolder.tv1.setText(note.getText());
                myViewHolder.tv2.setText(note.getComment());
            }
            return convertView;
        }
    }

    static final class MyViewHolder {

        @BindView(R.id.textView)
        TextView tv1;
        @BindView(R.id.textView2)
        TextView tv2;

        MyViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }


}
